package cn.yangyumanage.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import cn.yangyumanage.R;
import cn.yangyumanage.adapter.TagAllocationRvAdapter;
import cn.yangyumanage.application.DataApplication;
import cn.yangyumanage.bean.LargeClassification;
import cn.yangyumanage.callback.DataCallBack;
import cn.yangyumanage.http.HttpClient;
import cn.yangyumanage.model.TagAllocationHttpModel;
import cn.yangyumanage.util.Utils;


public class TagAllocationFragment extends Fragment {

    private Context context;

    private final TagAllocationHttpModel httpModel = new TagAllocationHttpModel();
    private View view;
    private AlertDialog.Builder builder;
    private SmartRefreshLayout srl_tag_allocation;
    private TagAllocationRvAdapter tagAllocationRvAdapter;
    private RecyclerView rv_tag_allocation;
    private NestedScrollView nsv_tag_allocation;

    public TagAllocationFragment(Context context) {
        this.context = context;
        builder = new AlertDialog.Builder(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_tag_allocation, container, false);
        initView();
        initData();
        return view;
    }

    private void initView() {
        rv_tag_allocation = view.findViewById(R.id.rv_tag_allocation);
        tagAllocationRvAdapter = new TagAllocationRvAdapter(getContext());
        rv_tag_allocation.setAdapter(tagAllocationRvAdapter);
        rv_tag_allocation.setLayoutManager(new LinearLayoutManager(getContext()));
        srl_tag_allocation = view.findViewById(R.id.srl_tag_allocation);
        srl_tag_allocation.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getAllLarge();
            }
        });
        nsv_tag_allocation = view.findViewById(R.id.nsv_tag_allocation);
    }

    private void initData() {
        getAllLarge();
    }

    //获取所有大分类
    private void getAllLarge() {
        httpModel.getAllLarge(new DataCallBack() {
            @Override
            public void getData(Object data) {
                List<LargeClassification> datas = Utils.<LargeClassification>objectToList(data);
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        tagAllocationRvAdapter.clearAllData();
                        tagAllocationRvAdapter.setData(datas);
                        srl_tag_allocation.finishRefresh();
                    }
                });
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!DataApplication.tagAllocationFlag) {
            nsv_tag_allocation.scrollTo(0, 0);
            srl_tag_allocation.autoRefresh();
            DataApplication.tagAllocationFlag = true;
        }
    }
}